package com.kafka;

import java.io.IOException;
import java.net.URI;
import java.util.Properties;

import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;

import com.kafka.conf.MyApplication;

/**
 * Main class.
 *
 */
public class Main {
    // Base URI the Grizzly HTTP server will listen on
    public static final String BASE_URI = "http://%s:%s/";
    private static String serverIp;
    private static String serverPort;
	
    /**
     * Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
     * @return Grizzly HTTP server.
     */
    public static HttpServer startServer() {
        // create a resource config that scans for JAX-RS resources and providers
        // in com.example package
    	final ResourceConfig rc = new MyApplication();

        // create and start a new instance of grizzly http server
        // exposing the Jersey application at BASE_URI
    	Object[] params = new Object[]{serverIp,serverPort};
        return GrizzlyHttpServerFactory.createHttpServer(URI.create(String.format(BASE_URI,params)), rc);
    }

    /**
     * Main method.
     * @param args
     * @throws IOException
     */
    @SuppressWarnings("deprecation")
	public static void main(String[] args) throws IOException {
    	loadProperties();
        final HttpServer server = startServer();
        Object[] params = new Object[]{serverIp,serverPort};
        System.out.println(String.format("Jersey app started with WADL available at "
                + "%sapplication.wadl\nHit enter to stop it...", String.format(BASE_URI,params)));
        System.in.read();
        server.stop();
    }
    
    /**
     * 加载服务器配置
     */
    private static void loadProperties(){
        Properties properties = new Properties();  
        try {
        	properties.load(ClassLoader
					.getSystemResourceAsStream("sysconfig.properties"));
			serverIp = properties.getProperty("server_ip");
			serverPort = properties.getProperty("server_port");
		} catch (IOException e) {
			e.printStackTrace();
		} 
    }
}

